~ chicken-core (chicken-5) /manual/Module (chicken locative)


 1[[tags: manual]]
 2[[toc:]]
 3
 4== Module (chicken locative)
 5
 6A ''locative'' is an object that points to an element of a containing object,
 7much like a ''pointer'' in low-level, imperative programming languages like ''C''. The element can
 8be accessed and changed indirectly, by performing access or change operations
 9on the locative. The container object can be computed by calling the
10{{locative->object}} procedure.
11
12Locatives may be passed to foreign procedures that expect pointer arguments.
13
14The following procedures are provided by the {{(chicken locative)}}
15module.
16
17=== make-locative
18
19<procedure>(make-locative OBJ [INDEX])</procedure>
20
21Creates a locative that refers to the element of the non-immediate object
22{{OBJ}} at position {{INDEX}}. {{OBJ}} may be a vector, pair, string, blob,
23SRFI-4 number-vector, or record structure. {{INDEX}} should be a fixnum.
24{{INDEX}} defaults to 0.
25
26
27=== make-weak-locative
28
29<procedure>(make-weak-locative OBJ [INDEX])</procedure>
30
31Creates a ''weak'' locative. Even though the locative refers to an element of a container object,
32the container object will still be reclaimed by garbage collection if no other references
33to it exist.
34
35
36=== locative?
37
38<procedure>(locative? X)</procedure>
39
40Returns {{#t}} if {{X}} is a locative, or {{#f}} otherwise.
41
42
43=== locative-ref
44
45<procedure>(locative-ref LOC)</procedure>
46
47Returns the element to which the locative {{LOC}} refers. If the containing
48object has been reclaimed by garbage collection, an error is signalled.
49
50 (locative-ref (make-locative "abc" 1)) ==> #\b
51
52=== locative-set!
53
54<procedure>(locative-set! LOC X)</procedure><br>
55<procedure>(set! (locative-ref LOC) X)</procedure>
56
57Changes the element to which the locative {{LOC}} refers to {{X}}.
58If the containing
59object has been reclaimed by garbage collection, an error is signalled.
60
61
62=== locative->object
63
64<procedure>(locative->object LOC)</procedure>
65
66Returns the object that contains the element referred to by {{LOC}} or
67{{#f}} if the container has been reclaimed by garbage collection.
68
69 (locative->object (make-locative "abc" 1)) ==> "abc"
70
71---
72Previous: [[Module (chicken load)]]
73
74Next: [[Module (chicken memory)]]
Trap